package android.Spider;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ThreadDisplay extends ListActivity
{
	public static String s_strUser = "User";
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		Bundle bundle = getIntent().getExtras();
		TopicThread thrd = new TopicThread();
		thrd.fromBundle(bundle);
		m_listAdapter.SetThread(thrd);
		
		setListAdapter(m_listAdapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
	    super.onCreateOptionsMenu(menu);
	    
	    menu.add(0, 0, "Reply to thread", R.drawable.replythread);
	    menu.add(0, 1, "Private message to selected", R.drawable.privatemsg);
	    return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item)
	{
	    if (item.getId() == 0)
	    {
	    	Intent intent = new Intent(this, ThreadReply.class);
			intent.putExtras(m_listAdapter.m_thread.toBundle());
			startSubActivity(intent, 0);
	    	return true;
	    }
	    else if (item.getId() == 1)
	    {
	    	Intent intent = new Intent(this, PrivateMessageSend.class);
	    	final int nPos = getSelectedItemPosition();
	    	if (nPos >= 0)
	    	{
		    	TopicThread.Issuer issuer = m_listAdapter.m_thread.m_lstPosts.get(nPos);
				intent.putExtra(s_strUser, issuer.m_strName);
				startSubActivity(intent, 0);
		    	return true;
	    	}
	    }
	    
	    return false;
	}

	class ThreadDisplayAdapter extends BaseAdapter
    {
    	private TopicThread m_thread;
    	
    	void SetThread(TopicThread thrd) { m_thread = thrd; this.notifyDataSetChanged(); }
    	
        public int getCount() { return m_thread.m_lstPosts.size(); }

        public Object getItem(int position) { return position; }

        public long getItemId(int position) { return position; }

        public View getView(int position, View convertView, ViewGroup parent)
        {
        	if (convertView == null)
        		convertView = getViewInflate().inflate(R.layout.threaddisplay, null, null);

        	// Title
        	TextView tv = (TextView)convertView.findViewById(R.id.ThreadDisplayTitle);
        	tv.setVisibility(position == 0 ? View.VISIBLE : View.GONE);
        	if (position == 0)
        	{
        		tv.setText(m_thread.m_strThreadName);
        		tv = (TextView)convertView.findViewById(R.id.ThreadDisplayRepliedBy);
        		tv.setText("Created by");
        	}
        	
        	// Issuer
        	TopicThread.Issuer issuer = m_thread.m_lstPosts.get(position);

        	// Text
        	tv = (TextView)convertView.findViewById(R.id.ThreadDisplayText);
        	tv.setText(issuer.m_strText);
        	
        	// Name
        	tv = (TextView)convertView.findViewById(R.id.ThreadDisplayByName);
        	int nPos = issuer.m_strName.indexOf('@');
        	String str = issuer.m_strName.substring(0, nPos);
        	tv.setText(str);

        	// Date
        	tv = (TextView)convertView.findViewById(R.id.ThreadDisplayByDate);
        	tv.setText(issuer.m_strTime);
        	
        	ProfileUser profile = SpiderPreferences.Instance(ThreadDisplay.this).FindProfile(issuer.m_strName);
        	((ImageView)convertView.findViewById(R.id.ThreadAvatarImage)).setImageResource(ProfileUser.idImages[profile.m_nAvatar]);

        	return convertView;
        }
    };

    ThreadDisplayAdapter m_listAdapter = new ThreadDisplayAdapter();

}
